package com.ruiysoft.controller.integralmdse.util;

import com.ruiysoft.controller.integralmdse.vo.InnrIntegralMdseVo;
import com.ruiysoft.controller.integralmdse.vo.IntegralMdseSaveVo;
import com.ruiysoft.controller.integralmdse.vo.IntegralMdseVo;
import com.ruiysoft.entity.integralmdse.MerchandiseSkuMPRela;
import com.ruiysoft.entity.mdse.MerchandiseRuleValueRela;
import com.ruiysoft.pub.consts.EnumUtil;
import com.ruiysoft.service.integralmdse.consts.MdseMPType;
import com.ruiysoft.util.NumberUtils;
import com.ruiysoft.util.RegularUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class IntegralMdseUtil {
    public static List<MerchandiseSkuMPRela> voToBList(IntegralMdseSaveVo body, Long shopId) {
        if(body==null){
            throw new RuntimeException("无保存数据");
        }
        //校验
        List<MerchandiseSkuMPRela> list = body.getList().stream()
                .map(vo->new MerchandiseSkuMPRela()
                        .setIntegral(vo.getIntegral())
                        .setShopId(shopId)
                        .setSkuId(Objects.requireNonNull(body.getSkuId(),"skuId不能为空"))
                        .setMemberLevel(Optional.ofNullable(vo.getMLv()).orElse(1))
                        .setType(Objects.requireNonNull(MdseMPType.getEnum(body.getType()),"会员支付类型不能为空").getIndex())
                        .setPrice(valiSkuPrice(vo.getPrice(),EnumUtil.Required.NO))
        ).peek(IntegralMdseUtil::vali).collect(Collectors.toList());

        Optional<Long> i = list.stream().collect(Collectors.groupingBy(a->a.getSkuId()+a.getMemberLevel(),Collectors.counting())).values().stream().max((a, b)-> (int) (a-b));
        if(i.orElse(1L)>1){
            throw new RuntimeException("同sku，会员级别只能有一个设置");
        }
        return list;
    }

    private static void vali(MerchandiseSkuMPRela bean) {
        if(Objects.equals(bean.getType(),MdseMPType.M_TO_I.getIndex())&&(bean.getIntegral()==null||bean.getPrice()==null)){
            throw new RuntimeException(MdseMPType.M_TO_I.getName()+":积分与金额必填");
        }else if(bean.getIntegral()==null&&bean.getPrice()==null){
            throw new RuntimeException(Objects.requireNonNull(MdseMPType.getEnum(bean.getType())).getName()+":积分与金额必填一个");
        }
    }


    private static Long valiSkuPrice(String s, EnumUtil.Required required) throws RuntimeException {
        String price = s;
        switch (required) {
            case YES:
                if(StringUtils.isBlank(price)){
                    throw new IllegalArgumentException("金额不能为空");
                }
                if (!RegularUtil.isMoney(price))
                    throw new IllegalArgumentException("金额错误");
                return Long.parseLong(NumberUtils.changeY2F(price));
            case NO:
                if (price != null) {
                    if (!RegularUtil.isMoney(price))
                        throw new IllegalArgumentException("金额错误");
                    return Long.parseLong(NumberUtils.changeY2F(price));
                }
                return null;
        }
        return null;
    }

    public static IntegralMdseSaveVo BListToVo(List<MerchandiseSkuMPRela> list) {
        final Integer[] type = {null};
        final Long[] skuId = {null};
        List<IntegralMdseVo> voList = list.stream().peek(bean->{if(type[0] ==null) type[0] =bean.getType();if(skuId[0] ==null) skuId[0] =bean.getSkuId();})
                .map(bean->new IntegralMdseVo()
                        .mLv(bean.getMemberLevel())
                        .integral(bean.getIntegral())
                        .id(bean.getId())
                        .type(bean.getType())
                        .price(NumberUtils.changeF2Y2(Optional.ofNullable(bean.getPrice()).map(Object::toString).orElse(null)))
                ).sorted(Comparator.comparingInt(IntegralMdseVo::getMLv)).collect(Collectors.toList());
        return new IntegralMdseSaveVo().type(type[0]).list(voList).skuId(skuId[0]);
    }


    public static InnrIntegralMdseVo BToInnrVo(MerchandiseSkuMPRela bean) {
        return new InnrIntegralMdseVo()
                .mLv(bean.getMemberLevel())
                .integral(bean.getIntegral())
                .id(bean.getId())
                .type(bean.getType())
                .price(bean.getPrice());
    }

    public static List<MerchandiseSkuMPRela> voListToBList(List<IntegralMdseSaveVo> body, Long shopId) {
        List<MerchandiseSkuMPRela> list = body.stream().filter(Objects::nonNull).flatMap(a->a.getList().stream()
                .map(vo->new MerchandiseSkuMPRela()
                        .setIntegral(vo.getIntegral())
                        .setShopId(shopId)
                        .setSkuId(Objects.requireNonNull(a.getSkuId(),"skuId不能为空"))
                        .setMemberLevel(Optional.ofNullable(vo.getMLv()).orElse(1))
                        .setType(Objects.requireNonNull(MdseMPType.getEnum(a.getType()),"会员支付类型不能为空").getIndex())
                        .setPrice(valiSkuPrice(vo.getPrice(),EnumUtil.Required.NO))
                ).peek(IntegralMdseUtil::vali)).collect(Collectors.toList());

        Optional<Long> i = list.stream().collect(Collectors.groupingBy(a->a.getSkuId()+a.getMemberLevel(),Collectors.counting())).values().stream().max(Comparator.comparingLong(a->a));
        if(i.orElse(1L)>1){
            throw new RuntimeException("同sku，会员级别只能有一个设置");
        }
        return list;
    }
}
